import { scrollMixin } from "utils/mixin";
export default {
  components: {},
  mixins: [scrollMixin], //加载更多
  data() {
    return {
      locationStatus: false, //定位开关
      goodsId: null,
      storeName: "",
      longitude: "", //经纬度
      latitude: "",
      timer: "",
      storeListByGoods: []
    };
  },
  activated() {
    this.goodsId = Number(this.$route.query.goodsId);
    this.longitude = this.$route.query.longitude;
    this.latitude = this.$route.query.latitude;
    if (this.longitude === "" && this.latitude === "") {
      this.locationStatus = false;
    } else {
      this.locationStatus = true;
    }
    this.getStoreListByGoods();
  },
  filters: {
    kmUnit(value) {
      let val = parseInt(value);
      let v;
      if (typeof val === "number" && !isNaN(val)) {
        if (val >= 1000) {
          v = (val / 1000).toFixed(2) + "km";
        } else {
          v = val + "m";
        }
      } else {
        v = "0m";
      }
      return v;
    }
  },
  methods: {
    searchChange(value) {
      this.storeName = value;
      // this.setData({ storeName: detail });
      // 防抖
      if (this.timer) clearTimeout(this.timer);
      this.timer = setTimeout(() => {
        this.debounce(this.getStoreListByGoods());
      }, 500);
    },
    navigation(...params) {
      let [latitude, longitude, storeName] = [...params];
      let point = this.fun.bd_decrypt(longitude, latitude);
      this.fun.goToWXAdress(point, storeName);
    },
    debounce(fn, delay) {
      let last, deferTimer;
      return function(args) {
        let that = this;
        let _args = arguments;
        let now = +new Date();
        if (last && now < last + delay) {
          clearTimeout(deferTimer);
          deferTimer = setTimeout(function() {
            last = now;
            fn.apply(that, _args);
          }, delay);
        } else {
          last = now;
          fn.apply(that, _args);
        }
      };
    },
    callStore(phone) {
      window.location.href = "tel://" + phone;
    },
    getStoreListByGoods() {
      let json = {
        goods_id: this.goodsId,
        longitude: this.longitude,
        latitude: this.latitude,
        name: this.storeName
      };
      $http.post("plugin.travel-around.frontend.goods.goods.getShopAddressByGoods", json, "正在获取").then(
        response => {
          if (response.result == 1) {
            let res = response.data;
            this.storeListByGoods = res;
          } else {
            this.$dialog.alert({ message: response.msg });
          }
        },
        function(response) {
          // console.log(response);
          // error callback
        }
      );
    }
  }
};
